﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Caiji.UserControls
{
    /// <summary>
    /// Ring.xaml 的交互逻辑
    /// </summary>
    public partial class Ring : UserControl
    {
        public Ring()
        {
            InitializeComponent();
            SizeChanged += Changed;
        }



        public int Percent
        {
            get { return (int)GetValue(PercentProperty); }
            set { SetValue(PercentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Percent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PercentProperty =
            DependencyProperty.Register("Percent", typeof(int), typeof(Ring), new PropertyMetadata(0));
        //画圆环
        void Drug()
        {
            //圆环的半径 是 当前页面 宽的一般，比如border 里面有个 textbox,则 RenderSize.Width = border.width
            grid.Width = Math.Min(RenderSize.Width,RenderSize.Width);
            double raduis = grid.Width / 2;
            //半径
            double x = raduis + (raduis - 3) * Math.Cos((Percent%100*3.6 - 90)*Math.PI/180) ;
            double y = raduis + (raduis - 3) * Math.Sin((Percent % 100 * 3.6 - 90) * Math.PI / 180);
            int tes = Percent < 50 ? 0 : 1;
            //移动路径
            string pathStr = $"M{raduis+0.01} 3A{raduis-3} {raduis - 3} 0 {tes} 1 {x} {y}";
            //进行类型转换
            var con = TypeDescriptor.GetConverter(typeof(Geometry));

            path.Data = (Geometry) con.ConvertFromString(pathStr);

        }
        //当屏幕大小发生改变
        private void Changed(object sender, SizeChangedEventArgs e)
        {
            Drug();
        }
    }
}
